﻿package com.webdesign.service.impl;

import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.webdesign.dao.jdbc.TbTermSQL;
import com.webdesign.dao.mapper.TbTermMapper;
import com.webdesign.entity.TbTerm;
import com.webdesign.entity.TbTermExample;
import com.webdesign.service.ITbTermService;

@Service("TbTermService")
public class TbTermService implements ITbTermService {

	@Autowired
	private TbTermMapper tbTermMapper;
	/*
	 * 返回所有学年的信息
	 */
	@Override
	public List<TbTerm> showTerms() {
		TbTermExample tbTermExample = new TbTermExample();
		tbTermExample.createCriteria().getAllCriteria();
		tbTermExample.setOrderByClause("CreateTime desc");
		List<TbTerm> terms = tbTermMapper.selectByExample(tbTermExample);
		return terms;
	}

	/*
	 * 根据学年编号获取学年名称
	 */
	@Override
	public String getTermName(String termid) {
		TbTerm tbTerm = tbTermMapper.selectByPrimaryKey(termid);
		return tbTerm.getTermname();
	}

	/*
	 * 判断是否存在同名的学年，若存在则返回学年编号，若不存在则新建学年返回学年编号
	 */
	@Override
	public String jugeTerm(String termname) {
		TbTermExample tbTermExample = new TbTermExample();
		tbTermExample.createCriteria().andTermnameEqualTo(termname);
		List<TbTerm> tbTermList = tbTermMapper.selectByExample(tbTermExample);
		if(tbTermList.size()==0){
			TbTermSQL tbTermSQL = new TbTermSQL();
			String maxtermid = tbTermSQL.getMaxTermid();
			String termid = String.valueOf(Integer.parseInt(maxtermid)+1);
			
			TbTerm tbTerm = new TbTerm();
			tbTerm.setTermid(termid);
			tbTerm.setTermname(termname);
			tbTerm.setCreatetime(new Date());
			tbTermMapper.insertSelective(tbTerm);
			return termid;
		}else{
			return tbTermList.get(0).getTermid();
		}
	}

}
